package cn.ylz.ylmall.common.qiniuyun;



import cn.ylz.ylmall.common.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import javax.validation.Valid;
import java.io.IOException;
import java.io.InputStream;

@Slf4j
@RestController
@Api(tags = "6.1. 文件上传云存储")
@RequestMapping("/v1/upload")
public class UploadController {

    /**
     * 文件上传
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("")
    @ApiOperation("文件上传")
    public JsonResult upload(@Valid MultipartFile file) throws IOException {
        //得的文件上传的名称
        String name = file.getOriginalFilename();
        String fileName = name +"_"+System.currentTimeMillis();
        System.out.println(fileName);
        //文件上传至云存储
        InputStream inputStream =file.getInputStream();
        String protectName = "img";

        Qiniuyun qiniuyun = new Qiniuyun();
        String url = qiniuyun.upFile(inputStream,fileName,protectName);
        //上传至云存储完毕
        return JsonResult.ok(url);

    }

}
